#include "requestcontenthelper.h"

#include <QCryptographicHash>
#include <QMap>
#include <QTime>

QString RequestContentHelper::generateRequestContent(const QString& url, const QMap<QString, QString>& paramsWithKey)
{
    return url + "?" + generateParamsContent(paramsWithKey);
}

QString RequestContentHelper::generateSalt()
{
    QString salt;
    qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));
    for (int i = 0; i < 10; i++) {
        int num = qrand() % 10;
        salt += QString::number(num);
    }

    return salt;
}

QString RequestContentHelper::generateSign(const QString& stringToSign)
{
    return QCryptographicHash::hash(stringToSign.toUtf8(), QCryptographicHash::Md5).toHex();
}

QString RequestContentHelper::generateParamsContent(const QMap<QString, QString>& paramsWithKey)
{
    QStringList params;
    for (const auto& key : paramsWithKey.keys())
        params << key + "=" + paramsWithKey.value(key);

    return params.join("&");
}

Language RequestContentHelper::languageStringToType(const QString& languageString)
{
    if (languageString == LANGUAGE_EN)
        return Language::En;

    if (languageString == LANGUAGE_ZH)
        return Language::Zh;

    return Language::Unknow;
}

QString RequestContentHelper::languageTypeToString(Language languageType)
{
    switch (languageType) {
    case Language::En:
        return LANGUAGE_EN;
    case Language::Zh:
        return LANGUAGE_ZH;
    default:
        return "";
    }
}
